<?php
namespace app\common\controller;


use OSS\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\Core\OssException;
use think\Exception;


require_once ROOT_PATH.'extend/aliyuncs/oss-sdk-php/src/OSS/OssClient.php';
require_once ROOT_PATH.'extend/aliyuncs/oss-sdk-php/src/OSS/Core/OssUtil.php';
require_once ROOT_PATH.'extend/aliyuncs/oss-sdk-php/src/OSS/Credentials/StaticCredentialsProvider.php';
require_once ROOT_PATH.'extend/aliyuncs/oss-sdk-php/src/OSS/Credentials/EnvironmentVariableCredentialsProvider.php';

 class OssService
{
    private $accessKeyId = 'LTAI5tQTkQnrZdL9UFEBe3Fj';
    private $accessKeySecret = 'qJDr6UCJsl5mwWE3btyVq9V3KlKP1o';
    private $endpoint = 'https://oss-cn-beijing.aliyuncs.com';
    private $bucket = 'wwwbimeitop';

    protected function getOssClient()
    {
        try {
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            return $ossClient;
        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return null;
        }
    }

    public function uploadVideo($filePath, $object = null)
    {
        $ossClient = $this->getOssClient();
        if ($ossClient === null) return false;

        $bucket = $this->bucket;
        $localfile = $filePath;


        $options = array(
            OssClient::OSS_PART_SIZE => 10 * 1024 * 1024, //设置分片大小
        );

        try {
            $res = $ossClient->multiuploadFile($bucket, $object, $localfile, $options);

        } catch (OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return false;
        }
        return $res['info']['url'];
    }


    public function uploadFile($filePath, $object = null){
        $ossClient = $this->getOssClient();
        if ($ossClient === null) return false;
        $bucket = $this->bucket;

        try {
            $res = $ossClient->uploadFile($bucket, $object, $filePath);

        } catch (OssException $e) {
            //printf(__FUNCTION__ . ": FAILED\n");
            //printf($e->getMessage() . "\n");
            //return false;
            throw new Exception($e->getMessage());
        }
        return $res['info']['url'];
        //return $res['info']['url'].'?x-oss-process=image/format,heic';
    }

     public function thumb1($filePath, $object = null){
         $ossClient = $this->getOssClient();
         if ($ossClient === null) return false;
         $bucket = $this->bucket;

         try {
             $res = $ossClient->uploadFile($bucket, $object, $filePath);

         } catch (OssException $e) {
             printf(__FUNCTION__ . ": FAILED\n");
             printf($e->getMessage() . "\n");
             return false;
         }
         return $res['info']['url'].'?x-oss-process=style/suoxiao4030';
     }

     public function thumb2($filePath, $object = null){
         $ossClient = $this->getOssClient();
         if ($ossClient === null) return false;
         $bucket = $this->bucket;

         try {
             $res = $ossClient->uploadFile($bucket, $object, $filePath);

         } catch (OssException $e) {
             printf(__FUNCTION__ . ": FAILED\n");
             printf($e->getMessage() . "\n");
             return false;
         }
         return $res['info']['url'].'?x-oss-process=style/suoxiao240160';
     }

     public function thumb3($filePath, $object = null){
         $ossClient = $this->getOssClient();
         if ($ossClient === null) return false;
         $bucket = $this->bucket;

         try {
             $res = $ossClient->uploadFile($bucket, $object, $filePath);

         } catch (OssException $e) {
             printf(__FUNCTION__ . ": FAILED\n");
             printf($e->getMessage() . "\n");
             return false;
         }
         return $res['info']['url'].'?x-oss-process=style/suoxiao600400';
     }
}

